﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CipherLib;
using CipherLib.Cipher;
using CipherLib.Cipher.Xml;
using CipherLib.Cipher.SMIME;

namespace CryptoLib.FileTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string decrypted;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);
                string encrypted;

                using (StreamReader sr = File.OpenText(openFileDialog1.FileName))
                {
                    Encoding en = sr.CurrentEncoding;
                    encrypted = sr.ReadToEnd();
                }
                decrypted = lib.DecryptString(encrypted);
                txtFile.Text = decrypted;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);

                string encrypted = lib.EncryptString(txtFile.Text);

                using (TextWriter sw = File.CreateText(saveFileDialog1.FileName))
                {
                    sw.NewLine = Environment.NewLine;
                    sw.Write(encrypted);
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string decrypted;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string encrypted;

                using (StreamReader sr = File.OpenText(openFileDialog1.FileName))
                {
                    Encoding en = sr.CurrentEncoding;
                    encrypted = sr.ReadToEnd();
                }
                txtFile.Text = encrypted;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);
         
                string encrypted = txtFile.Text;
                encrypted = encrypted.Replace("\n", Environment.NewLine);
                using (TextWriter sw = File.CreateText(saveFileDialog1.FileName))
                {
                    sw.Write(encrypted);
                }
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string decrypted;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                string encrypted;

                using (StreamReader sr = File.OpenText(openFileDialog1.FileName))
                {
                    Encoding en = sr.CurrentEncoding;
                    encrypted = sr.ReadToEnd();
                }
                txtFile.Text = encrypted;
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            string decrypted;
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);
                string encrypted;

                using (StreamReader sr = File.OpenText(openFileDialog1.FileName))
                {
                    Encoding en = sr.CurrentEncoding;
                    encrypted = sr.ReadToEnd();
                }
                decrypted = lib.DecryptString(encrypted);
                txtFile.Text = decrypted;
            }
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);

                string encrypted = txtFile.Text;
                encrypted = encrypted.Replace("\n", Environment.NewLine);
                using (TextWriter sw = File.CreateText(saveFileDialog1.FileName))
                {
                    sw.Write(encrypted);
                }
            }
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                ICipher lib = new CipherSMime(this.txtCN.Text);

                string encrypted = lib.EncryptString(txtFile.Text);

                using (TextWriter sw = File.CreateText(saveFileDialog1.FileName))
                {
                    sw.NewLine = Environment.NewLine;
                    sw.Write(encrypted);
                }
            }
        }


        private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e)
        {

        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            object o = toolStripSplitButton1.DefaultItem;
        }

    }
}
